import ipaddress

# 区分IPv4和IPv6地址
def Check_IP_Type(ip):
    try:
        addr = ipaddress.ip_address(ip)
        if isinstance(addr, ipaddress.IPv4Address):
            print(f"{ip} 是 IPv4 地址。")
            return 4
        elif isinstance(addr, ipaddress.IPv6Address):
            print(f"{ip} 是 IPv6 地址。")
            return 6
    except ValueError:
        print(f"{ip} 不是有效的 IP 地址。")
        return False

def IP_v4_Calculator(ip_address,mask_length,subnet_mask_length):
    try:
        network = ipaddress.IPv4Network(f"{ip_address}/{mask_length}")
        subnet_mask = mask_length + subnet_mask_length
        network_subnet = ipaddress.IPv4Network(f"{ip_address}/{subnet_mask}")
        subnets = list(network.subnets(prefixlen_diff=subnet_mask_length))
        print(f"网络: {network}")
        print(f"网络地址: {network.network_address}")
        print(f"子网掩码: {network.netmask}")
        print(f"最大地址数: {network.num_addresses}")
        print(f"可用地址数: {network.num_addresses - 2 if network.num_addresses > 2 else 0}")
        print(f"第一个可用地址: {str(network.network_address + 1)}" if network.num_addresses > 2 else '第一个可用地址：N/A')
        print(f"最后一个可用地址: {str(network.broadcast_address - 1)}" if network.num_addresses > 2 else '最后一个可用地址：N/A')
        print(f"广播地址: {network.broadcast_address}")
        print("---------------------------------------------------------------------------------------------")
        print(f"子网位数：{subnet_mask_length}")
        print(f"划分子网数量：{2 ** subnet_mask_length}")
        if subnet_mask != 32:
            print(f"主机位数：{32-subnet_mask}")
            print(f"子网可用地址数量：{network_subnet.num_addresses - 2 if network.num_addresses > 2 else 0}")
        print("---------------------------------------------------------------------------------------------")
        for index,subnet in enumerate(subnets):
            print(f"子网{index+1}: {subnet}")
            subnet_network = ipaddress.IPv4Network(subnet)
            print(f"网络地址: {subnet_network.network_address}")
            print(f"子网掩码: {subnet_network.netmask}")
            print(f"最大地址数: {subnet_network.num_addresses}")
            if subnet_mask == 31:
                print(f"可用地址数: {subnet_network.num_addresses - 2 if subnet_network.num_addresses > 2 else 0}")
                print(f"第一个可用地址: {str(subnet_network.network_address + 1)}" if subnet_network.num_addresses > 2 else '第一个可用地址：N/A')
                print(f"最后一个可用地址: {str(subnet_network.broadcast_address - 1)}" if subnet_network.num_addresses > 2 else '最后一个可用地址：N/A')
            elif subnet_mask != 32:
                print(f"可用地址数: {subnet_network.num_addresses - 2 if subnet_network.num_addresses > 2 else 0}")
                print(f"第一个可用地址: {str(subnet_network.network_address + 1)}" if subnet_network.num_addresses > 2 else '第一个可用地址：N/A')
                print(f"最后一个可用地址: {str(subnet_network.broadcast_address - 1)}" if subnet_network.num_addresses > 2 else '最后一个可用地址：N/A')
                print(f"广播地址: {subnet_network.broadcast_address}")
            print("---------------------------------------------------------------------------------------------")

    except ValueError:
        print("输入无效，请检查后重新输入。")

def IP_v6_Calculator(ip_address,mask_length,subnet_mask_length):
    try:
        network = ipaddress.IPv6Network(f"{ip_address}/{mask_length}")
        subnet_mask = mask_length + subnet_mask_length
        network_subnet = ipaddress.IPv6Network(f"{ip_address}/{subnet_mask}")
        subnets = list(network.subnets(prefixlen_diff=subnet_mask_length))
        print(f"网络: {network}")
        print(f"网络地址: {network.network_address}")
        print(f"子网掩码: {network.netmask}")
        print(f"最大地址数: {network.num_addresses}")
        print(f"可用地址数: {network.num_addresses - 2 if network.num_addresses > 2 else 0}")
        print(f"第一个可用地址: {str(network.network_address + 1)}" if network.num_addresses > 2 else '第一个可用地址：N/A')
        print(f"最后一个可用地址: {str(network.broadcast_address - 1)}" if network.num_addresses > 2 else '最后一个可用地址：N/A')
        print(f"广播地址: {network.broadcast_address}")
        print("---------------------------------------------------------------------------------------------")
        print(f"子网位数：{subnet_mask_length}")
        print(f"划分子网数量：{2 ** subnet_mask_length}")
        if subnet_mask != 128:
            print(f"主机位数：{128 - subnet_mask}")
            print(f"子网可用地址数量：{network_subnet.num_addresses - 2 if network.num_addresses > 2 else 0}")
        print("---------------------------------------------------------------------------------------------")
        for index, subnet in enumerate(subnets):
            print(f"子网{index + 1}: {subnet}")
            subnet_network = ipaddress.IPv6Network(subnet)
            print(f"网络地址: {subnet_network.network_address}")
            print(f"子网掩码: {subnet_network.netmask}")
            print(f"最大地址数: {subnet_network.num_addresses}")
            if subnet_mask == 127:
                print(f"可用地址数: {subnet_network.num_addresses - 2 if subnet_network.num_addresses > 2 else 0}")
                print(f"第一个可用地址: {str(subnet_network.network_address + 1)}" if subnet_network.num_addresses > 2 else '第一个可用地址：N/A')
                print(f"最后一个可用地址: {str(subnet_network.broadcast_address - 1)}" if subnet_network.num_addresses > 2 else '最后一个可用地址：N/A')
            elif subnet_mask != 128:
                print(f"可用地址数: {subnet_network.num_addresses - 2 if subnet_network.num_addresses > 2 else 0}")
                print(f"第一个可用地址: {str(subnet_network.network_address + 1)}" if subnet_network.num_addresses > 2 else '第一个可用地址：N/A')
                print(f"最后一个可用地址: {str(subnet_network.broadcast_address - 1)}" if subnet_network.num_addresses > 2 else '最后一个可用地址：N/A')
                print(f"广播地址: {subnet_network.broadcast_address}")
            print("---------------------------------------------------------------------------------------------")

    except ValueError:
        print("输入无效，请检查后重新输入。")

if __name__ == '__main__':
    # 用户输入的IP地址和掩码长度和子网长度
    ip_address = input("请输入IP地址: ")
    ip_version = Check_IP_Type(ip_address)
    if ip_version == False:
        exit()
    mask_length = int(input("请输入掩码长度: "))
    if ip_version == 4 and mask_length not in range(0,33):
        print("掩码长度输入无效，请检查后重新输入。")
        exit()
    elif ip_version == 6 and mask_length not in range(0, 129):
        print("掩码长度输入无效，请检查后重新输入。")
        exit()
    else:
        print("输入有效。")
    subnet_mask_length = int(input("请输入子网长度: "))
    if ip_version == 4 and mask_length + subnet_mask_length >32:
        print("子网长度输入无效，请检查后重新输入。")
        exit()
    elif ip_version == 6 and mask_length + subnet_mask_length >128:
        print("子网长度输入无效，请检查后重新输入。")
        exit()
    else:
        print("输入有效。")
    print("---------------------------------------------------------------------------------------------")
    if ip_version == 4:
        IP_v4_Calculator(ip_address,mask_length,subnet_mask_length)
    elif ip_version == 6:
        IP_v6_Calculator(ip_address, mask_length, subnet_mask_length)
